<?php
// .-----------------------------------------------------------------------------------
// |  Software: [HDPHP framework]
// |   Version: 2013.01
// |      Site: http://www.hdphp.com
// |-----------------------------------------------------------------------------------
// |    Author: 向军 <houdunwangxj@gmail.com>
// | Copyright (c) 2012-2013, http://houdunwang.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
// |   License: http://www.apache.org/licenses/LICENSE-2.0
// '-----------------------------------------------------------------------------------

/**
 * SESSION文件驱动
 * @package     session
 * @subpackage  driver
 * @author      后盾向军 <houdunwangxj@gmail.com>
 */


class FileSession extends SessionAbstract {

    public $sessionSavePath; //SESSION储存路径
    private $sessionFile; //当前用户SESSION文件

    function __construct() {
        parent::__construct();
        $this->sessionSavePath = PATH_SESSION;
    }

    function open() {
        return true;
    }

    function read($sid) {
        $this->sessionFile = $this->sessionSavePath . '/' . $sid;
        if (!is_file($this->sessionFile)) {
            return false;
        }
        return file_get_contents($this->sessionFile);
    }

    function write($sid, $data) {
        return file_put_contents($this->sessionFile, $data) ? true : false;
    }

    function destroy($sid) {
        if (is_file($this->sessionFile)) {
            unlink($this->sessionFile);
        }
    }

    function gc() {
        foreach (glob($this->sessionSavePath . "/*") as $file) {
            if (filemtime($file) + $this->sessionLifeTime < time()) {
                unlink($file);
            }
        }
        return true;
    }

}

?>
